package com.itheima.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.domain.HomeBrand;
import com.itheima.mapper.HomeBrandMapper;
import com.itheima.service.SmsHomeBrandService;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class SmsHomeBrandServiceImpl implements SmsHomeBrandService {
    @Autowired
    HomeBrandMapper smsHomeBrandMapper;

    //分页查询推荐品牌
    @Override
    public PageBeanVo list(Integer pageNum, Integer pageSize, String brandName, Integer recommendStatus) {
        Page<HomeBrand> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<HomeBrand> lqw = new LambdaQueryWrapper<>();
        lqw.like(StrUtil.isNotEmpty(brandName), HomeBrand::getBrandName, brandName);
        lqw.eq(recommendStatus != null, HomeBrand::getRecommendStatus, recommendStatus);
        lqw.orderByAsc(HomeBrand::getSort);
        Page<HomeBrand> brandPage = smsHomeBrandMapper.selectPage(page, lqw);
        return new PageBeanVo(pageNum, pageSize, brandPage.getTotal(), brandPage.getRecords());
    }

    //添加首页推荐品牌
    @Override
    public void create(HomeBrand homeBrand) {
        smsHomeBrandMapper.insert(homeBrand);
    }
    //批量删除推荐品牌

    @Override
    public void delete(Long id) {
        smsHomeBrandMapper.deleteById(id);
    }

    //修改推荐品牌排序
    @Override
    public void updateSort(Long id, Integer sort) {
        LambdaQueryWrapper<HomeBrand> lqw = new LambdaQueryWrapper<>();
        lqw.eq(HomeBrand::getId, id);
        HomeBrand smsHomeBrand = smsHomeBrandMapper.selectById(id);
        smsHomeBrand.setSort(sort);
        smsHomeBrandMapper.updateById(smsHomeBrand);
    }

    //获取品牌集合
    @Override
    public List<HomeBrand> smsHomeBrands() {
        LambdaQueryWrapper<HomeBrand> lqw = new LambdaQueryWrapper<>();
        List<HomeBrand> smsHomeBrands = smsHomeBrandMapper.selectList(lqw);
        return smsHomeBrands;
    }

    //批量修改推荐品牌状态
    @Override
    public void updateRecommendStatus(Long id, Integer recommendStatus) {
        HomeBrand smsHomeBrand = smsHomeBrandMapper.selectById(id);
        smsHomeBrand.setRecommendStatus(recommendStatus);
        smsHomeBrandMapper.updateById(smsHomeBrand);
    }

}
